package com.example.mvp.Ui.Fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends RxFragment {

    protected Activity mActivity;
    protected Context context;
    private View rootView;
    private Unbinder unbinder;


//    @Override
//    public void onAttach(Context context) {
//        super.onAttach(context);
//        mActivity= (Activity) context;
//        this.context=context;
//
//    }
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity= (Activity) context;
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (rootView==null){
            rootView=inflater.inflate(createLayoutId(),null);
        }
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    protected abstract int createLayoutId();


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    protected abstract void initData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (rootView!=null){
            ((ViewGroup)rootView.getParent()).removeView(rootView);
        }
        if (unbinder!=null){
            unbinder.unbind();
        }
    }
}
